//
//  PEMStandardSettingView.swift
//  RelationshipIntegration
//
//  Created by iOS-Czz on 2023/11/16.
//

import UIKit
import ProgressHUD

class PEMStandardSettingView: UIView {

    @IBOutlet var stepTF: UITextField!
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */
    override func awakeFromNib() {
        super.awakeFromNib()
        if let defaultStep = UserDefaults.standard.value(forKey: "DefaultSteps") as? Double {
            self.stepTF.text = String(format: "%.0f", defaultStep)
        } else {
            // Handle the case where the value is nil or not a String
            // For example, set a default value or display an error message
            self.stepTF.text = "0"
        }

    }
    @IBAction func clickCancelAction(_ sender: Any) {
        self.removeFromSuperview()
    }
    
    @IBAction func clickOkAction(_ sender: Any) {
        self.endEditing(true)
        if self.stepTF.text!.isEmpty {
            ProgressHUD.showError("Please enter number of exercise steps")
            return
        }
        ProgressHUD.showSuccess()
        UserDefaults.standard.setValue(Double(self.stepTF.text!), forKey: "DefaultSteps")
        let notificationName = Notification.Name("StandardStepSetting")
        NotificationCenter.default.post(name: notificationName, object: nil, userInfo: nil)
        self.removeFromSuperview()
    }
}
